package wt.ai.chatagentservice.domain;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 通用返回结果封装类
 *
 * @param <T> 数据负载类型
 */
public class R<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    // 常用状态码常量
    public static final int SUCCESS = 200;
    public static final int BAD_REQUEST = 400;
    public static final int UNAUTHORIZED = 401;
    public static final int FORBIDDEN = 403;
    public static final int NOT_FOUND = 404;
    public static final int INTERNAL_SERVER_ERROR = 500;

    // 状态码
    private int code;
    // 返回消息
    private String msg;
    // 数据负载
    private T data;
    // 时间戳
    private long timestamp;
    // 扩展信息
    private Map<String, Object> extra;

    public R() {
        this.timestamp = System.currentTimeMillis();
    }

    public R(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.timestamp = System.currentTimeMillis();
    }

    /**
     * 成功结果（无数据）
     */
    public static <T> R<T> ok() {
        return new R<>(SUCCESS, "操作成功", null);
    }

    /**
     * 成功结果（带数据）
     */
    public static <T> R<T> ok(T data) {
        return new R<>(SUCCESS, "操作成功", data);
    }

    /**
     * 成功结果（带消息和数据）
     */
    public static <T> R<T> ok(String msg, T data) {
        return new R<>(SUCCESS, msg, data);
    }

    /**
     * 失败结果
     */
    public static <T> R<T> fail() {
        return new R<>(INTERNAL_SERVER_ERROR, "操作失败", null);
    }

    /**
     * 失败结果（带消息）
     */
    public static <T> R<T> fail(String msg) {
        return new R<>(INTERNAL_SERVER_ERROR, msg, null);
    }

    /**
     * 失败结果（带状态码和消息）
     */
    public static <T> R<T> fail(int code, String msg) {
        return new R<>(code, msg, null);
    }

    /**
     * 设置状态码
     */
    public R<T> code(int code) {
        this.code = code;
        return this;
    }

    /**
     * 设置消息
     */
    public R<T> msg(String msg) {
        this.msg = msg;
        return this;
    }

    /**
     * 设置数据
     */
    public R<T> data(T data) {
        this.data = data;
        return this;
    }

    /**
     * 添加扩展信息
     */
    public R<T> putExtra(String key, Object value) {
        if (this.extra == null) {
            this.extra = new HashMap<>();
        }
        this.extra.put(key, value);
        return this;
    }

    /**
     * 判断是否成功
     */
    public boolean isSuccess() {
        return this.code == SUCCESS;
    }

    // Getter 和 Setter 方法
    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public Map<String, Object> getExtra() {
        return extra;
    }

    public void setExtra(Map<String, Object> extra) {
        this.extra = extra;
    }

    @Override
    public String toString() {
        return "R{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                ", timestamp=" + timestamp +
                ", extra=" + extra +
                '}';
    }
}